<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="row panel panel-default table-responsive">
    <div class="panel-heading">
        <span class="fa fa-list-ul" aria-hidden="true"></span>
        <b>组织机构列表</b>
    </div>
    <c:if test="${empty list}">
        <div style="text-align: center;padding: 20px;"><h4>暂无相关数据</h4></div>
    </c:if>
    <c:if test="${not empty list}">
        <table class="table table-bordered table-hover">
            <thead>
            <tr>
                <th>名称</th>
                <shiro:hasRole name="<%=SystemConstant.SYS_SUPER_ADMINCODE%>">
                    <th>类型</th>
                </shiro:hasRole>
                <th>编码</th>
                <th>状态</th>
                <th style="width: 150px;">创建日期</th>
                <th>描述</th>
                <th width="220">操作</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${list}" var="org" varStatus="status">
                <tr>
                    <td>
                        <c:if test="${status.index==0}">
                            <i class="fa fa-flag" style="color: #098d38;"></i>
                            ${org.orgName}
                        </c:if>
                        <c:if test="${status.index!=0}">
                            <label style="margin-left: ${30*org.levels}px;">${org.orgName}</label>
                        </c:if>
                    </td>
                    <shiro:hasRole name="<%=SystemConstant.SYS_SUPER_ADMINCODE%>">
                        <td>
                            <c:if test="${org.agent=='sys'}">
                                系统类型
                            </c:if>
                            <c:if test="${org.agent!='sys'}">
                                商务类型
                            </c:if>
                        </td>
                    </shiro:hasRole>
                    <td>${org.orgCode}</td>
                    <td>
                        <c:if test="${org.state==1}">正常</c:if>
                        <c:if test="${org.state!=1}">禁用</c:if>
                    </td>
                    <td>${ fn:substring(org.createDate ,0,19)}</td>
                    <td>${org.remark}</td>
                    <td>
                        <c:if test="${org.levels==0}">
                            <shiro:hasPermission name="system_org_add">
                                <button class="btn btn-xs btn-success addOrg" data-toggle="modal" data-target="#addOrg"
                                        data-org="${org}" title="添加公司或者合作商">
                                    <i class="ace-icon fa fa-plus-square bigger-120"></i>添加公司或者合作商
                                </button>
                            </shiro:hasPermission>
                        </c:if>
                        <c:if test="${org.levels!=0}">
                            <shiro:hasPermission name="system_org_add">
                                <button class="btn btn-xs btn-success addOrg" data-toggle="modal" data-target="#addOrg"
                                        data-org="${org}" title="添加子部门">
                                    <i class="ace-icon fa fa-plus-square bigger-120"></i>子部门
                                </button>
                            </shiro:hasPermission>
                            <c:if test="${status.index!=0}">
                                <shiro:hasPermission name="system_org_update">
                                    <button class="btn btn-xs btn-info updateOrg" data-toggle="modal" data-target="#addOrg"
                                            data-org="${org}" title="修改">
                                        <i class="ace-icon fa fa-edit bigger-120"></i>修改
                                    </button>
                                </shiro:hasPermission>
                                <shiro:hasPermission name="system_org_del">
                                    <button class="btn btn-xs btn-danger delOrg" data-toggle="modal" data-target="#delOrg"
                                            data-orgId="${org.orgId}" title="删除">
                                        <i class="ace-icon fa fa-trash-o bigger-120"></i>删除
                                    </button>
                                </shiro:hasPermission>
                                <label class="hide"><img src="${root}/components/ztree/css/img/loading.gif"/></label>
                            </c:if>
                        </c:if>
                    </td>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </c:if>
</div>
<div id="addOrg" class="modal fade" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">添加组织机构</h4>
            </div>
            <form id="addOrgForm" class="form-horizontal" action="${root}/system/org/add" method="post"
                  onsubmit="return doSubmit();">
                <div class="modal-body">
                    <input type="hidden" name="pId" id="pId">
                    <input type="hidden" name="orgId" id="orgId">
                    <input type="hidden" name="levels" id="levels">
                    <input type="hidden" name="agent" id="agent">
                    <div class="form-group labelOrgName">
                        <label class="col-sm-2 control-label col-xs-3">上级机构:</label>
                        <div class="col-sm-10 col-xs-9">
                            <p class="form-control-static"></p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="orgName" class="col-sm-2 control-label col-xs-3">机构名称:</label>
                        <div class="col-sm-10 col-xs-9">
                            <input type="text" name="orgName" id="orgName" class="form-control" required
                                   placeholder="组织机构名称" maxlength="15">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="orgCode" class="col-sm-2 control-label col-xs-3">机构编码:</label>
                        <div class="col-sm-10 col-xs-9">
                            <input type="text" name="orgCode" id="orgCode" class="form-control" required
                                   placeholder="组织机构编码" maxlength="30">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="status" class="col-sm-2 control-label col-xs-3">机构状态:</label>
                        <div class="col-sm-10 col-xs-9">
                            <label class="radio-inline" style="padding-top: 4px;">
                                <input type="radio" name="status" id="status" value="0">启用
                            </label>
                            <label class="radio-inline" style="padding-top: 4px;">
                                <input type="radio" name="status" value="1" >禁用
                            </label>
                        </div>
                    </div>
                    <shiro:hasRole name="<%=SystemConstant.SYS_SUPER_ADMINCODE%>">
                        <div class="form-group adminAgent">
                            <label for="adminAgent" class="col-sm-2 control-label col-xs-3">机构类型:</label>
                            <div class="col-sm-10 col-xs-9">
                                <label class="radio-inline" style="padding-top: 4px;">
                                    <input type="radio" name="adminAgent" id="adminAgent" value="sys">系统类型
                                </label>
                                <label class="radio-inline" style="padding-top: 4px;">
                                    <input type="radio" name="adminAgent" value="business" >商务类型
                                </label>
                            </div>
                        </div>
                    </shiro:hasRole>
                    <div class="form-group">
                        <label for="remark" class="col-sm-2 control-label col-xs-3">机构备注:</label>
                        <div class="col-sm-10 col-xs-9">
                            <textarea name="remark" id="remark" class="form-control" rows="5"
                                      placeholder="机构备注,最多添加100个字" maxlength="100"></textarea>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary btn-sm" id="submit">添加</button>
                </div>
            </form>
        </div>
    </div>
</div>
<div id="delOrg" class="modal fade" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">删除机构</h4>
            </div>
            <div class="modal-body">
                <input type="hidden" name="delOrgId" id="delOrgId" value="0">
                你确定要删除该机构吗？
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary btn-sm doDelRole">确定</button>
            </div>
        </div>
    </div>
</div>
<script>
    function doSubmit() {
        if ($("#adminAgent").val() && $("#adminAgent").val() != "") {
            $("#agent").val($('input:radio[name=adminAgent]:checked').val());
        }
    }
    var sleObj=null;
    //删除组织机构
    $('.delOrg').click(function () {
        $("#delOrgId").val($(this).attr('data-orgId'))
        sleObj = $(this);

    });
    $('.doDelRole').click(function () {
        $(sleObj).parents('td').find('label').removeClass('hide');
        $('#delOrg').modal('hide');
        $.ajax({
            type: 'POST',
            url: root + '/system/org/del',
            data: {orgId: $("#delOrgId").val()},
            success: function (data) {
                var dataObj = data;
                if (dataObj.status == 1) {
                    $(sleObj).parents('tr').remove();
                }
                showMes(dataObj.status, dataObj.msg);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $(sleObj).parents('td').find('label').addClass('hide');
                showMes("0", "系统繁忙,请稍后再试");
            }
        });
    });
    $(".addOrg").click(function () {
        $("#addOrg h4").html("添加组织机构");
        $("#submit").html("添加");
        $('#addOrgForm').find('input[type=hidden]').val('');
        $('#addOrgForm').find('input[type=text]').val('');
        $("#addOrgForm").find('textarea').val('');
        $("#addOrgForm").find('input:radio[name=status]')[0].checked = true;
        $("#addOrgForm .labelOrgName").show();
        $("#addOrgForm").attr('action',root+'/system/org/add');
        var org = eval("(" + $(this).attr('data-org') + ")");
        $("#pId").val(org.orgId)
        $("#levels").val(parseInt(org.levels) + 1);
        $('.form-control-static').html(org.orgName);
        if (org.agent != "sys") {
            if ($(".adminAgent")) {
                $(".adminAgent").hide();
            }
            $("#agent").val(org.agent);
        }else{
            $(".adminAgent").show();
        }
        $("#addOrgForm").find('input:radio[name=adminAgent]')[1].checked = true;
    });
    $(".updateOrg").click(function () {
        $('#addOrgForm').find('input[type=hidden]').val('');
        $('#addOrgForm').find('input[type=text]').val('');
        $("#addOrgForm").find('textarea').val('');
        $("#addOrgForm").find('input:radio[name=status]')[0].checked = true;
        $("#addOrgForm").attr('action',root+'/system/org/update');
        $("#addOrgForm .labelOrgName").hide();
        var org = eval("(" + $(this).attr('data-org') + ")");
        $("#addOrg h4").html("修改"+org.orgName);
        $("#submit").html("修改");
        $("#agent").val(org.agent);
        if(org.status==1){
            $("#addOrgForm").find('input:radio[name=status]')[1].checked = true;
        }
        if (org.agent != "sys") {
            $(".adminAgent").hide();
        }else{
            $(".adminAgent").show();
        }
        $("#addOrgForm").find('input:radio[name=adminAgent]')[1].checked = true;
        if(org.agent=='sys'){
            $("#addOrgForm").find('input:radio[name=adminAgent]')[0].checked = true;
        }
        $("#orgId").val(org.orgId);
        $("#orgName").val(org.orgName);
        $("#orgCode").val(org.orgCode);
        $("#remark").val(org.remark);
    });
</script>